using System;
using Rhino.Security.Interfaces;
using WineCellar.Business.Entities;
using WineCellar.Business.ServiceInterfaces;
using WineCellar.Core.Services;

namespace WineCellar.Business.Services
{
    public class WineService :EntityService<Wine>
    {
        private readonly IUserService _userService;
        private readonly IAuthorizationRepository _authRepos;

        public WineService(IUserService _userService, IAuthorizationRepository _authRepos)
        {
            this._userService = _userService;
            this._authRepos = _authRepos;
        }

        public void Save(Wine instance)
        {
            instance.DateAdded = DateTime.Now;            
            base.Save(instance);

            //put wine in a security group based on the region
            var secGroup = _authRepos.GetEntitiesGroupByName("Wines of region: " + instance.Region.Name) ??
                           _authRepos.CreateEntitiesGroup("Wines of region: " + instance.Region.Name);
           _authRepos.AssociateEntityWith(instance,secGroup);
        }
    }
}